//
//  RegisterVC.m
//  doubleEduStudents
//
//  Created by 盛洋 on 2021/1/5.
//  Copyright © 2021 shengYang. All rights reserved.
//

#import "RegisterVC.h"
#import "DoneInforVC.h"

@interface RegisterVC ()<UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *topHeight;
@property (strong, nonatomic) IBOutlet UILabel *titleLab;
@property (strong, nonatomic) IBOutlet UITextField *phoneField;
@property (strong, nonatomic) IBOutlet UITextField *codeField;
@property (strong, nonatomic) IBOutlet UIButton *sendBtn;

@property (strong, nonatomic) IBOutlet UIButton *redBtn;
@property (strong, nonatomic) IBOutlet UILabel *redLab;

@property (strong, nonatomic) IBOutlet UIButton *serviceBtn;
@property (strong, nonatomic) IBOutlet UIButton *secrectBtn;

@property (strong, nonatomic) IBOutlet UIButton *registerBtn;
@property (strong, nonatomic) IBOutlet UIButton *touristBtn;
@property (nonatomic ,assign) BOOL isSelects;//是否选中

@end

@implementation RegisterVC{
    NSString *agree;//是否同意协议
    RequestServices *service;
    int _count; // 倒计时数字
    NSTimer *_timer; // 定时器
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.topHeight.constant = StatusBarAndNavHeight;
    APPViewBorderRadius(self.sendBtn, 5, .1, [UIColor clearColor]);
    APPViewBorderRadius(self.registerBtn, 5, .1, [UIColor clearColor]);
    agree = @"0";
    [self setFontSize];
    service = [[RequestServices alloc]initWithDelegate:self];
    self.phoneField.delegate = self;
    self.codeField.delegate = self;

}

-(void)setFontSize{
    self.titleLab.font = AutoLabFont1;
    self.phoneField.font = self.codeField.font = self.redLab.font = self.sendBtn.titleLabel.font = self.serviceBtn.titleLabel.font = self.secrectBtn.titleLabel.font = self.registerBtn.titleLabel.font = self.touristBtn.titleLabel.font = AutoLabFont2;
}

//同意选中按钮
- (IBAction)agreeBtn:(UIButton *)sender {
    [self.view endEditing:YES];
    sender.selected = !sender.selected;
    self.isSelects = sender.selected;
    if (self.isSelects) {
        [self.redBtn setImage:[UIImage imageNamed:@"quan_s"] forState:UIControlStateSelected];
        agree = @"1";
    }else{
        [self.redBtn setImage:[UIImage imageNamed:@"quan_n"] forState:UIControlStateSelected];
        agree = @"0";
    }
}

//发送验证码
- (IBAction)sendBtn:(UIButton *)sender {
    [self.view endEditing:YES];
    [self.phoneField resignFirstResponder];
    if (self.phoneField.text.length != 11) {
        [self ShowNewMsg:@"请填写正确的手机号码"];
        return;
    }
    [service getMobileCode:self.phoneField.text type:@"1" tag:8000];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
{  //string就是此时输入的那个字符textField就是此时正在输入的那个输入框返回YES就是可以改变输入框的值NO相反
    
    if ([string isEqualToString:@"\n"])  //按会车可以改变
    {
        return YES;
    }
    
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; //得到输入框的内容
    
    if (self.phoneField == textField)  //判断是否时我们想要限定的那个输入框
    {
        if ([toBeString length] > 11) { //如果输入框内容大于11则弹出警告
            textField.text = [toBeString substringToIndex:11];
            [self ShowNewMsg:@"手机号不能超过11位"];
            return NO;
        }
    }
    if (self.codeField == textField)  //判断是否时我们想要限定的那个输入框
    {
        if ([toBeString length] > 6) { //如果输入框内容大于11则弹出警告
            textField.text = [toBeString substringToIndex:6];
            [self ShowNewMsg:@"验证码不能超过6位"];
            return NO;
        }
    }
    return YES;
}


//注册按钮
- (IBAction)registerBtn:(id)sender {
    [self.view endEditing:YES];
    if (![self.phoneField.text CheckNSStringData]) {
        [self ShowNewMsg:@"请输入手机号"];
        return;
    }
    if (![self.codeField.text CheckNSStringData]) {
        [self ShowNewMsg:@"请输入验证码"];
        return;
    }
    
    if ([agree isEqualToString:@"0"]) {
        [self ShowNewMsg:@"请阅读并勾选协议"];
        return;
    }
    [service loginToApplication:self.phoneField.text captcha:self.codeField.text messageDeviceToken:@"" tag:8001];
}

#pragma mark - requestDelegate
-(void)getFinished:(NSDictionary *)obj tag:(long long)tag{
    
    if (tag == 8000) {
        _count = 60;
        [self handleTimer];
        NSUDSetPhone(self.phoneField.text);
        _timer = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(handleTimer) userInfo:nil repeats:YES];
    }
    if (tag == 8001) {
        NSUDSetValueWithKey(obj[@"token"],@"Token");
        DoneInforVC *vc = [[DoneInforVC alloc]init];
        [self PushToViewController:vc];
    }
}

-(void)getError:(NSError *)error tag:(long long)tag{
    [self ShowNewMsg:error.domain];
}

- (void)handleTimer
{
    
    if (_count > 0) {
        [self.sendBtn setUserInteractionEnabled:NO];
        [self.sendBtn setTitle:[NSString stringWithFormat:@"%ds",_count] forState:UIControlStateNormal];
    } else {
        [self.sendBtn setUserInteractionEnabled:YES];
        [self.sendBtn setTitle:[NSString stringWithFormat:@"重发验证码"] forState:UIControlStateNormal];
        
        [_timer invalidate];
    }
    _count -= 1;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
